package com.itcanteen.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcanteen.reggie.dto.DishDto;
import com.itcanteen.reggie.entity.Dish;
import com.itcanteen.reggie.entity.DishFlavor;
import com.itcanteen.reggie.mapper.CategoryDao;
import com.itcanteen.reggie.mapper.DishDao;
import com.itcanteen.reggie.mapper.DishFlavorDao;
import com.itcanteen.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2022/10/26 16:07
 */

@Service
public class DishServiceImpl implements DishService {


    @Autowired(required = false)
    DishDao dishDao;
    @Autowired(required = false)
    DishFlavorDao dishFlavorDao;

    @Autowired(required = false)
    CategoryDao categoryDao;


    @Override
    public void save(DishDto dishDto) {
//1. 补全菜品的数据
        dishDto.setStatus(1); // 1代表菜品启用  0代表下架商品
        dishDto.setSort(0);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        //2.保存菜品
        dishDao.insert(dishDto);


        //3.得到菜品的口味信息
        List<DishFlavor> flavorList = dishDto.getFlavors();
        //4. 给一个口味信息补全
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId()); //口味对应的菜品的id
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setIsDeleted(0);
        }
        //5. 批量插入菜品口味
        for (DishFlavor df:flavorList) {
            dishFlavorDao.insert(df);
        }
    }


    /**
     * 菜品分页列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findByPage(Integer page, Integer pageSize, String name) {
        Page<DishDto> pageInfo=new Page<>(page,pageSize);
        List<DishDto> list = dishDao.findAll(pageInfo,name);
        return pageInfo.setRecords(list);
    }

    /**
     * 根据菜品id查找菜品
     * @param id
     * @return
     */
    @Override
    public DishDto findById(Long id) {
        //1. 根据菜品的id查询菜品信息
        Dish dish = dishDao.findById(id);
        //2. 根据菜品的id查询菜品对应口味信息
        List<DishFlavor> dishFlavorList = dishFlavorDao.findByDishId(id);
        //3. 把菜品信息与口味信息封装成DishDto
        DishDto dishDto = new DishDto();
        //属性拷贝  源
        BeanUtils.copyProperties(dish,dishDto);
        //设置口味信息
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(DishDto dishDto) {
        //1. 更新时间
        dishDto.setUpdateTime(LocalDateTime.now());
        //2.更新菜品
        dishDao.updateDish(dishDto);


        //3.删除菜品的口味
        QueryWrapper<DishFlavor> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("dish_id",dishDto.getId());
        dishFlavorDao.delete(objectQueryWrapper);

        //4. 得到当前菜品最新口味，然后补全数据
        List<DishFlavor> flavorList = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId()); //口味对应的菜品的id
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setIsDeleted(0);

            dishFlavorDao.insert(dishFlavor);
        }
        //5. 批量插入口味信息
      //  dishFlavorDao.sa

    }

}
